package com.jt.assistant.ui

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ToastUtils
import com.jt.assistant.R
import com.jt.assistant.adapter.PicAdapter
import com.jt.assistant.base.GlobalConfig.JTMS_URL_BASE
import com.jt.assistant.bean.Param
import com.jt.assistant.viewmodel.HomeViewModel
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_sku_pic_detail.*

/**
 * 已上传图片
 */
class SkuPicDetailActivity : BaseActivity<HomeViewModel>() {
    private val mAdapter by lazy {
        PicAdapter(
            ArrayList<String>()
        )
    }

    override fun layoutId(): Int = R.layout.activity_sku_pic_detail

    override fun initView(savedInstanceState: Bundle?) {
        with(rv_skuPicDetail) {
            layoutManager = GridLayoutManager(context, 3)
            adapter = mAdapter
        }
    }

    override fun initData() {
        val params = Param::class.java.newInstance()
        params.channelOrderNo = ""
        params.sku = intent.getStringExtra("SKU_NO").toString()
        viewModel.getWareFileBySku(params)
    }

    override fun initListener() {
        mAdapter.setOnItemClickListener { adapter, view, position ->
            startActivity(Intent(this@SkuPicDetailActivity, PreActivity::class.java).apply {
                putExtra("IMAGEPATH", GsonUtils.toJson(mAdapter.data))
                putExtra("POSITION", position)
                putExtra("SKUCHECK", true)
            })
        }

        viewModel.mGetWareFileBySku.observe(this, {
            if (it.isSuccess()) {
                if (it.data.isEmpty()) ToastUtils.showShort("未上传过图片")

                it.data.forEach {
                    mAdapter.data.add(JTMS_URL_BASE + it.replace("/api/jtms-logistics/", ""))
                }
                mAdapter.notifyDataSetChanged()
            }
        })
    }

}